package com.falcon.service.impl;

import java.util.List;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import cn.hutool.core.bean.BeanUtil;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.swan.mybatis.condition.Condition;
import com.falcon.mvc.domain.BasePager;
import com.falcon.core.utils.BeanUtils;

import com.falcon.domain.po.DeptPO;
import com.falcon.domain.dos.DeptDO;
import com.falcon.domain.req.DeptQuery;
import com.falcon.domain.req.DeptRequest;

import com.falcon.mapper.IDeptMapper;
import com.falcon.service.IDeptService;


@Slf4j
@Service
public class DeptService implements IDeptService {

    @Autowired
    private IDeptMapper deptMapper;

    @Override
    public Long save(DeptRequest request){
        DeptPO deptPO = BeanUtil.copyProperties(request, DeptPO.class);

        this.deptMapper.insertNotNull(deptPO);

        return deptPO.getId();
    }

    @Override
    public Boolean update(Long id, DeptRequest request){

        DeptPO deptPO = BeanUtil.copyProperties(request, DeptPO.class);
        deptPO.setId(id);

        return this.deptMapper.updateNotNull(deptPO);
    }

    @Override
    public Boolean deleteById(Long id) {

        return this.deptMapper.deleteById(id);
    }

    @Override
    public DeptDO queryById(Long id){

        DeptPO deptPO = this.deptMapper.selectById(id);

        DeptDO deptDO = BeanUtil.copyProperties(deptPO, DeptDO.class);

        return deptDO;
    }


    @Override
    public List<DeptDO> queryList(DeptQuery request){

        Condition condition = request.toCondition();

        List<DeptPO> poList = this.deptMapper.selectList(condition);

        List<DeptDO> doList = BeanUtil.copyToList(poList, DeptDO.class);

        return doList;
    }

    @Override
    public BasePager<DeptDO> queryPager(DeptQuery request){


        PageInfo<DeptPO> pageInfo = PageHelper.startPage(request.getPage(), request.getPageSize())
                .doSelectPageInfo(() -> this.deptMapper.selectList(request.toCondition()));

        return BeanUtils.toPager(pageInfo, DeptDO.class);
    }

    @Override
    public int batchDelete(List<Long> ids) {

        return this.deptMapper.deleteByIds(ids);
    }


}

